#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/122c4t/10252012_challenge_107_easy_all_possible_decodings/

DECODE = (1 .. 26).to_a.zip(('a' .. 'z').to_a).inject({}) { |r, (k, v)| r.merge(k => v) }

def decodings(s, curr = "", coll = [])
    if s.empty? && ! curr.empty?
        coll << curr
    else
        (1 .. s.size).each do |i|
            sub = s.slice(0, i)
            rem = s.slice(i, s.size)

            break unless DECODE.has_key?(sub.to_i)

            decodings(rem, curr + DECODE[sub.to_i], coll)
        end
    end

    return coll
end

ARGV.each { |s| puts "decoding '#{s}':" ; puts decodings(s).sort.map { |d| "  #{d}"}.join("\n") ; puts }
